//------------------------------------------------------------------------------
// <copyright file="InstallUtil.cs" company="carlosag.net">
//     Copyright (c) carlosag.net.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.Web.Administration;

namespace Cloudbase.Iis.Setup
{
    internal static class InstallUtil {

        public static void AddUIModuleProvider(string name, string type) {

            RemoveUIModuleProvider(name);

            using (ServerManager mgr = new ServerManager()) {

                // First register the Module Provider 
                Configuration adminConfig = mgr.GetAdministrationConfiguration();

                ConfigurationSection moduleProvidersSection = adminConfig.GetSection("moduleProviders");
                ConfigurationElementCollection moduleProviders = moduleProvidersSection.GetCollection();
                if (FindByAttribute(moduleProviders, "name", name) == null) {
                    ConfigurationElement moduleProvider = moduleProviders.CreateElement();
                    moduleProvider.SetAttributeValue("name", name);
                    moduleProvider.SetAttributeValue("type", type);
                    moduleProviders.Add(moduleProvider);
                }

                //// Now register it so that all Sites have access to this module
                //adminConfig = mgr.GetAdministrationConfiguration(new WebConfigurationMap(), "");
                ConfigurationSection modulesSection = adminConfig.GetSection("modules");
                ConfigurationElementCollection modules = modulesSection.GetCollection();
                if (FindByAttribute(modules, "name", name) == null) {
                    ConfigurationElement module = modules.CreateElement();
                    module.SetAttributeValue("name", name);
                    modules.Add(module);
                }

                mgr.CommitChanges();
            }
        }

        private static ConfigurationElement FindByAttribute(ConfigurationElementCollection collection, string attributeName, string value) {
            foreach (ConfigurationElement element in collection) {
                if (String.Equals((string)element.GetAttributeValue(attributeName), value, StringComparison.OrdinalIgnoreCase)) {
                    return element;
                }
            }

            return null;
        }

        public static void RemoveUIModuleProvider(string name) {
            using (ServerManager mgr = new ServerManager()) {
                // First remove it from the sites
                Configuration adminConfig = mgr.GetAdministrationConfiguration();
                ConfigurationSection modulesSection = adminConfig.GetSection("modules");
                ConfigurationElementCollection modules = modulesSection.GetCollection();
                ConfigurationElement module = FindByAttribute(modules, "name", name);
                if (module != null) {
                    modules.Remove(module);
                }

                // now remove the ModuleProvider
                //adminConfig = mgr.GetAdministrationConfiguration(new WebConfigurationMap(), "");
                ConfigurationSection moduleProvidersSection = adminConfig.GetSection("moduleProviders");
                ConfigurationElementCollection moduleProviders = moduleProvidersSection.GetCollection();
                ConfigurationElement moduleProvider = FindByAttribute(moduleProviders, "name", name);
                if (moduleProvider != null) {
                    moduleProviders.Remove(moduleProvider);
                }

                mgr.CommitChanges();
            }
        }
    }
}
